package com.qc.controller;

import java.util.List;
import java.util.Random;

import org.springframework.stereotype.Controller;

import com.qc.common.Constants;
import com.qc.common.FailCodeHandle;
import com.qc.entity.ReturnResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class BaseController {

    protected static JSONObject returnSuccessResultEnt(Object entity){
        return JSONObject.fromObject(entity);
    }
    
	protected static String returnSuccessResult(Object entity){
		ReturnResult rr = new ReturnResult();
		rr.setReturnType(Constants.SUCCESS);
		rr.setReturnCode(JSONObject.fromObject(entity).toString());
		return JSONObject.fromObject(rr).toString();
	}
	
	protected static String returnFailedResult(FailCodeHandle errorCode){
		ReturnResult rr = new ReturnResult();
		rr.setReturnType(Constants.FAILED);
		rr.setReturnCode(errorCode.toString());
		return JSONObject.fromObject(rr).toString();
	}
	
	protected static String returnFailedResult(String errorCode){
		ReturnResult rr = new ReturnResult();
		rr.setReturnType(Constants.FAILED);
		rr.setReturnCode(errorCode);
		return JSONObject.fromObject(rr).toString();
	}
	
	protected static String returnSuccessResult(String str){
		ReturnResult rr = new ReturnResult();
		rr.setReturnType(Constants.SUCCESS);
		rr.setReturnCode(str);
		return JSONObject.fromObject(rr).toString();
	}
	
	protected static String returnSuccessResult(List list){
		ReturnResult rr = new ReturnResult();
		rr.setReturnType(Constants.SUCCESS);
		rr.setReturnCode(JSONArray.fromObject(list).toString());
		return JSONObject.fromObject(rr).toString();
	}
	
	public static void main(String[] args) {
		Random rnd = new Random();
		int n = 10000000 + rnd.nextInt(90000000);
		System.out.println(n);
	}
}
